package com.wiim.ui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.wiim.adapter.DoiThuongAdapter;
import com.wiim.adapter.ExchangeLstAdapter;
import com.wiim.adapter.PageAdapter;
import com.wiim.common.Utilities;
import com.wiim.handler.SHOPCmdHandler;
import com.wiim.interfaceapp.IServiceCommandResult;
import com.wiim.model.DoithuongModel;
import com.wiim.model.PageModel;
import com.wiim.model.ShopData;
import com.wiim.nhapnhap.AppGlobal;
import com.wiim.nhapnhap.EgoodApp;
import com.wiim.nhapnhap.R;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.DirectionalViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;

public class DoiThuongFragment extends Fragment implements IServiceCommandResult, OnPageChangeListener {
	// private GridView gvCategory;
	private ExchangeLstAdapter dataAdapter;
	ArrayList<ShopData> ExList = new ArrayList<ShopData>();
	ArrayList<DoithuongModel> doiThuongList = new ArrayList<DoithuongModel>();
	// private DirectionalViewPager pagerAdBanner;
	private PageAdapter adBannerAdapter;
	private ArrayList<PageModel> list = new ArrayList<PageModel>();
	ArrayList<Button> listButton = new ArrayList<Button>();
	// private LinearLayout lnChoise;
	private ListView lvDoiThuong;

	@Override
	public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.doithuong_list, container, false);
		initFeld(view);
		// initBanner();
		return view;
	}

	private void initBanner() {
		PageModel bannerModel;
		for (int i = 0; i < 6; i++) {
//			if (i % 2 != 0) {
//				bannerModel = new PageModel(R.drawable.bg_banner_home, "Shop");
//			} else {
//				bannerModel = new PageModel(R.drawable.pic, "Shop");
//			}

//			list.add(bannerModel);
		}
		adBannerAdapter = new PageAdapter(getActivity(), list);
		// pagerAdBanner.setAdapter(adBannerAdapter);
		// pagerAdBanner.setCurrentItem(0);
		// pagerAdBanner.setOnPageChangeListener(this);

		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) (Utilities.getGlobalVariable(getActivity()).device_width / 60),
				(int) (Utilities.getGlobalVariable(getActivity()).device_height / 50));
		params.weight = 1.0f;
		// LinearLayout lnTv;
		listButton = new ArrayList<Button>();
		for (int i = 0; i < 5; i++) {
			Button btnChoise = new Button(getActivity());
			if (i == 0) {
				btnChoise.setBackgroundResource(R.drawable.icon_vuongchoice);
			} else {
				btnChoise.setBackgroundResource(R.drawable.icon_vuong_none);
			}
			params.leftMargin = 5;
			btnChoise.setLayoutParams(params);
			listButton.add(btnChoise);
			// lnChoise.addView(btnChoise);
		}
		setAutoPager(0);
	}

	Handler handler = new Handler();

	public void setAutoPager(final int index) {
		handler.removeMessages(0);
		handler.postDelayed(new Runnable() {
			public void run() {
				// if (index < 6-1) {
				// pagerAdBanner.setCurrentItem(index + 1);
				// } else if (index == 6-1) {
				// pagerAdBanner.setCurrentItem(0);
				// }
			}
		}, 3000);
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initData();
	}

	private void initData() {
		Bundle b = new Bundle();
		b.putString(EgoodApp.REST_OBJECT_PUT, "*");
		AppGlobal.getInstance(DoiThuongFragment.this).startServiceCmd(SHOPCmdHandler.COMMAND_GET_SHOPLIST_BY_CATEGORYID, new SHOPCmdHandler(), b, true);
	}

	Integer[] image = { R.drawable.item_doithuong, R.drawable.item_do, R.drawable.item_tim, R.drawable.item_xam, R.drawable.item_xanh, R.drawable.item_xanhdatroi, R.drawable.item_xanhnhat };
	List<Integer> listAdd = new ArrayList<Integer>();

	private void bindData(ShopData[] exModel) {
		ExList = new ArrayList<ShopData>(Arrays.asList(exModel));
		// dataAdapter = new
		// ExchangeLstAdapter(DoiThuongFragment.this.getActivity(),
		// R.layout.doithuong_item, ExList);

		int count = 1;
		for (int i = 0; i < ExList.size(); i++) {
			if (i < image.length) {
				listAdd.add(image[i]);
			} else {
				listAdd.add(image[(i - (image.length * count))]);
			}
			count = (listAdd.size() / image.length);
		}
//		System.out.println("tinhvc size 1: " + listAdd.size());
//		System.out.println("tinhvc size 2: " + ExList.size());
		DoiThuongAdapter dataAdapter1 = new DoiThuongAdapter(getActivity(), ExList, listAdd);
		lvDoiThuong.setAdapter(dataAdapter1);
	}

	private void initFeld(View view) {
		lvDoiThuong = (ListView) view.findViewById(R.id.lvDoiThuong);
		// pagerAdBanner = (DirectionalViewPager)
		// view.findViewById(R.id.pagerAdBanner);
		// lnChoise = (LinearLayout) view.findViewById(R.id.lnChoise);
	}

	@Override
	public void processCommandResult(int commandCode, Object resultData) {
		// TODO Auto-generated method stub
		Bundle bResultData = (Bundle) resultData;

		switch (commandCode) {
		case SHOPCmdHandler.COMMAND_GET_SHOPLIST_BY_CATEGORYID: {
			ShopData[] exlst = (ShopData[]) bResultData.getSerializable(EgoodApp.REST_OBJECT_RETURN);
			bindData(exlst);
		}
			break;

		default:
			break;
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		for (int i = 0; i < 5; i++) {
			if (arg0 == i) {
				listButton.get(i).setBackgroundResource(R.drawable.icon_vuongchoice);
			} else {
				listButton.get(i).setBackgroundResource(R.drawable.icon_vuong_none);
			}
		}
		setAutoPager(arg0);

	}

	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub

	}

}
